<?php
namespace Modules\Activity\Http\Controllers\Client;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\Ticket;

use \Modules\User\Entities\User;

use \Modules\Activity\Entities\BaseActivityModel;
use \Modules\Agent\Entities\InviteRecord;
use \Modules\Agent\Entities\InviteRecord2;

use Validator;
use ThrowException;
use DB;
use Auth;
use Setting;

class TicketController extends Controller
{
    // 当前用户的剩余次数
    public function total(Request $req)
    {
        $rule = [
            'uuid' => ['required'],
            'node_type' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        if ( $req->node_type == 'choushang' ){
            $res = DB::select("select uuid from choushang_activity where title IN ( select title from choushang_activity where uuid = '{$req->uuid}' ) ");
            $res = object_to_array($res);
            $ticket = Ticket::where([
                'user_id' => $user->id
            ])->WhereIn('node_uuid' , array_column($res, 'uuid'))->first();
        }elseif($req->node_type == 'leitai'){
            $res = DB::select("select uuid from leitai_activity where title IN ( select title from leitai_activity where uuid = '{$req->uuid}' ) ");
            $res = object_to_array($res);
            $ticket = Ticket::where([
                'user_id' => $user->id
            ])->WhereIn('node_uuid' , array_column($res, 'uuid'))->first();
        }else{
            $ticket = Ticket::where([
                'user_id' => $user->id,
                'node_uuid' => $req->uuid
            ])->first();
        }

        $node = BaseActivityModel::getNode($req->node_type, [
            'uuid' => $req->uuid
        ]);

        $inviteTotal = InviteRecord::where([
            'direct_inviter_id' => $user->id,
            'invite_node_uuid' => $req->uuid
        ])->count();

        return [
            'title' => @$node->title,
            'invite_total' => $inviteTotal,
            'ticket_id' => @$ticket->id ?: 0,
            'stock' => @$ticket->stock ?: 0,
            'used_total' => @$ticket->used_total ?: 0,
            'rule' => $node->invite_rule,
            'share_rule' => $node->share_rule,
            'is_invite' => $node->is_invite_enabled,
            'is_share' => $node->is_share_enabled
        ];
    }

    public function total2(Request $req)
    {
        $rule = [
            'uuid' => ['required'],
            'node_type' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $ticket = Ticket::where([
            'user_id' => $user->id,
            'node_uuid' => $req->uuid
        ])->first();

        $node = BaseActivityModel::getNode($req->node_type, [
            'uuid' => $req->uuid
        ]);

        $inviteTotal = InviteRecord2::where([
            'direct_inviter_id' => $user->id,
            'invite_node_uuid' => $req->uuid
        ])->count();

        return [
            'title' => @$node->title,
            'invite_total' => $inviteTotal,
            'stock' => @$ticket->stock ?: 0,
            'used_total' => @$ticket->used_total ?: 0,
            'rule' => $node->invite_rule,
            'share_rule' => $node->share_rule,
            'is_invite' => $node->is_invite_enabled,
            'is_share' => $node->is_share_enabled
        ];
    }

    public function buyPreview(Request $req)
    {
        $rule = [
            'node_type' => ['required'],
            'node_uuid' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $item = BaseActivityModel::getNode($req->node_type, [
            'uuid' => $req->node_uuid
        ]);

        $info = [
            'title' => $item->title,
            'score_price' => $item->score_price
        ];

        return [
            'info' => $info
        ];
    }

    public function buy(Request $req)
    {
        $rule = [
            'node_type' => ['required'],
            'node_uuid' => ['required'],
            'total' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        $options = $req->all();

        $total = intVal($req->total);
        if ($total <= 0) {
            ThrowException::Conflict('请输入正确兑换次数~');
        }

        $item = BaseActivityModel::getNode($req->node_type, [
            'uuid' => $req->node_uuid
        ]);

        $scorePrice = $item->score_price;

        $totalPrice = $item->score_price * $total;

        DB::beginTransaction();

        if ($totalPrice) {
            $user->asset('score')->out($totalPrice, [
                'description' => '兑换次数(' . $item->title . ')'
            ]) ?: ThrowException::Conflict('余额不足,兑换失败~');

            // 加剩余可用活动次数
            Ticket::in($user, $item, $total, $totalPrice . Setting::scoreAlias() . '兑换');
        }

        DB::commit();

        $info = [
            'title' => $item->title
        ];

        return [
        ];
    }
}
